package com.wangsh.userscenter.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.PageInfoUtil;
import com.wangsh.userscenter.service.users.api.IUsersService;
import com.wangsh.userscenter.service.users.pojo.AUsers;

/**
 * 用户登陆后的主页面
 * 
 * @author wangsh_sxt
 *
 */
@Controller
@RequestMapping("/back/users/")
public class UsersBackController extends BaseController
{
	@Autowired
	private IUsersService usersService;

	/**
	 * 用户列表
	 * 
	 * @return
	 */
	@RequestMapping("/usersList.html")
	public String usersList(HttpServletRequest request)
	{
		/* 接收参数 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");

		if (keyword == null)
		{
			keyword = "";
		}
		keyword = keyword.trim();
		if (status == null)
		{
			status = "";
		}
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.parseDateTime(st);
			edDate = this.dateUtil.parseDateTime(ed);
		}

		/* 查询数据 */
		PageInfoUtil pageInfoUtil = this.setPageInfoCond(request);
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", "%" + keyword + "%");
		condMap.put("status", status);
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		List<AUsers> usersList = this.usersService.findCondListUsersService(pageInfoUtil, condMap);

		/* 存储结果 */
		request.setAttribute("usersList", usersList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/back/users/usersList";
	}

	/**
	 * 用户更新
	 * 
	 * @return
	 */
	@RequestMapping("/usersUpdate.html")
	public String usersUpdate(HttpServletRequest request)
	{
		String uid = request.getParameter("uid");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", uid);
		AUsers users = this.usersService.findOneUsersService(condMap);
		request.setAttribute("users", users);

		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			return "/back/users/usersUpdate";
		}
		return "/back/users/usersInfo";
	}

	/**
	 * 用户更新提交
	 * 
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit.html")
	public String usersUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		String uid = request.getParameter("uid");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", uid);
		AUsers users = this.usersService.findOneUsersService(condMap);

		boolean updateFlag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			String status = request.getParameter("status");

			users.setStatus(Byte.valueOf(status));
			updateFlag = true;
		}

		if (updateFlag)
		{
			users.setUpdateTime(new Date());
			JSONObject dbJSON = this.usersService.updateOneUsersService(users);
			if (dbJSON != null)
			{
				info = dbJSON.getString("info") + "";
			}
		}
		JSONObject resultJSON = this.returnJSON(request, info);
		this.printOut(request, response, resultJSON + "");
		return null;
	}

	/**
	 * 用户的批处理
	 * 
	 * @return
	 */
	@RequestMapping("/usersBatch.html")
	public String usersBatch(HttpServletRequest request, HttpServletResponse response)
	{
		return null;
	}
}
